package com.bdqn.struts.util;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
    private static SessionFactory factory=null;
    private static Session session=null;
    static {
        //1读取配置文件
        Configuration cfg=new Configuration().configure();
        //2.获取SessionFctory
        factory=cfg.buildSessionFactory();
            }
            //获取session
    public static Session getSession(){
        //使用getCurrentSession() 打开session后执行完毕会自动将session进行关闭,无需手动关闭
        //如果使用openSession()打开session,执行侯完毕必须手动将session进行关闭
        return factory.getCurrentSession();
    }

    //关闭
    public static void closeSession(Session session){
        if (session!=null){
            session.close();
        }
    }
}
